home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-02-26 | 7.5 KB | 280 lines | [TEXT/MPS ] |
- /*
- File: UTidyApplication.cp
-
- Contains: TTidyApplication::Free() and functions and member functions
- which it calls. For use with MacApp 3.1.
-
- Written by: Adam Wildavsky
-
- Copyright: © 1994 by Adam Wildavsky
-
- Change History (most recent first):
-
- 2/26/94 aw Added a To Do item. Added a comment. No code changes.
- 2/24/94 aw Trimmed blanks, added punctuation. No code changes.
- 2/3/94 aw Updated for MacApp 3.1b1
- 1/23/94 aw Baseline
-
- To Do:
- Free ClassDesc::fgClassDescListByID and ClassDesc::fgClassDescListByName.
- Clean up non-object allocations.
- */
-
-
- #ifndef __UTIDYAPPLICATION__
- #include "UTidyApplication.h"
- #endif
-
- #ifndef __MACAPP__
- #include <MacApp.h>
- #endif
-
-
- #pragma segment AClose
- static void DeInitUMenuMgr()
- {
- /*******
-
- Clean up the objects allocated by InitUMenuMgr, in reverse order of allocation.
-
- ********/
-
- extern TObject * gMenuTable; // Actually these are all TObject descendants,
- extern TObject * gCmdTable; // but this will work! If we had declared them
- extern TObject * gMenuIDList; // as their actual types we'd have cast them
- // to TObjects anyway for FreeIfObject().
-
- gMenuTable = FreeIfObject(gMenuTable);
- gCmdTable = FreeIfObject(gCmdTable);
- gMenuIDList = FreeIfObject(gMenuIDList);
- }
-
-
- #pragma segment AClose
- static void DeInitUClassDesc()
- {
- /*******
-
- An abortive attempt to clean up the objects allocated by
- ClassDesc::InitUClassDesc.
-
- In fact the static members fgClassDescListByID and fgClassDescListByName are
- private; we can't access them from here without modifying MacApp. If one were
- to modify MacApp one would probably make this a ClassDesc static method.
-
- As it turns out leaving them around is not much of a hindrance to us since
- Steve Jasik's "The Debugger" doesn't list them in its "Objects by …" displays.
-
- ********/
-
- #if FALSE
- ClassDesc::fgClassDescListByID =
- (TClassDescListByID *) FreeIfObject(ClassDesc::fgClassDescListByID);
- ClassDesc::fgClassDescListByName =
- (TClassDescListByName *) FreeIfObject(ClassDesc::fgClassDescListByName);
- #endif
- }
-
-
- #pragma segment AClose
- static void DeInitUObject()
- {
- /*******
-
- Clean up anything allocated allocated by InitUObject.
- This function exists solely to make the structure
- easier to understand and maintain. Each InitU… function
- which causes TObject allocations has a corresponding
- DeInitU… function, and they are called in reverse order
- of one another.
-
- ********/
-
- DeInitUClassDesc();
- }
-
-
- #pragma segment AClose
- static void DeInitUDialog()
- {
- /*******
-
- Clean up the objects allocated by InitUDialog, in reverse order of allocation.
-
- ********/
-
- // This test is necessary because gFloatingTEManager and gParamTxt
- // are -not- initialized to NULL when they are defined in UDialog.cp
- if (gUDialogInitialized)
- {
- gFloatingTEManager = (TFloatingTEManager *) FreeIfObject(gFloatingTEManager);
-
- extern TAssociation * gParamTxt; // It should be empty now
- gParamTxt = (TAssociation *) FreeIfObject(gParamTxt);
- }
- }
-
-
- #pragma segment AClose
- static void DeInitUAdorners()
- {
- /*******
-
- Clean up the objects allocated by InitUAdorners, in reverse order of allocation.
- They're only in reverse order for form's sake!
-
- This function wasn't necessary in MacApp 3.0.1 as the adorners were freed when
- "gGlobalContext" was freed. In MacApp 3.1 gGlobalContext no longer exists!
-
- ********/
-
- gSelectionAdorner = (TSelectionAdorner *) FreeIfObject(gSelectionAdorner);
- gResizeIconAdorner = (TResizeIconAdorner *) FreeIfObject(gResizeIconAdorner);
- gPrintAdorner = (TPrintAdorner *) FreeIfObject(gPrintAdorner);
- gHiliteAdorner = (THiliteAdorner *) FreeIfObject(gHiliteAdorner);
- gEraseAdorner = (TEraseAdorner *) FreeIfObject(gEraseAdorner);
- gDrawAdorner = (TDrawAdorner *) FreeIfObject(gDrawAdorner);
- gDimAdorner = (TDimAdorner *) FreeIfObject(gDimAdorner);
- }
-
-
- #pragma segment AClose
- static void DeInitUMacApp()
- {
- /*******
-
- Clean up the objects allocated by InitUMacApp, in reverse order of allocation.
-
- ********/
-
- DeInitUDialog();
- DeInitUMenuMgr();
- DeInitUAdorners();
-
- extern TDynamicArray * gSignatures;
- gSignatures = (TDynamicArray *) FreeIfObject(gSignatures);
-
- // This test wasn't necessary in MacApp 3.0. It is now because
- // we're dealing with pointers. Setting one pointer to NULL won't
- // set the other to NULL even if they were pointing to the same thing!
- if (gPrintHandler == gNullPrintHandler)
- {
- gPrintHandler = (TPrintHandler *) FreeIfObject(gPrintHandler);
- gNullPrintHandler = NULL;
- }
- else
- {
- gPrintHandler = (TPrintHandler *) FreeIfObject(gPrintHandler);
- gNullPrintHandler = (TPrintHandler *) FreeIfObject(gNullPrintHandler);
- }
-
- DeInitUObject();
- }
-
-
- #pragma segment AClose
- static void LastChance()
- {
- // Set a breakpoint here in order to check for memory leaks.
- // In Jasik do an "Objects by Time".
-
- #if qDebug
- ProgramBreak("It's time to check for memory leaks!");
- #endif
- }
-
-
- #pragma segment AClose
- void TTidyApplication::CleanUpAfterIApplication()
- {
- /*******
-
- Clean up the objects allocated by TApplication::IApplication, in reverse order of allocation.
-
- ********/
-
- gMenuBarManager = (TMenuBarManager *) FreeIfObject(gMenuBarManager);
- gMacAppDependencies = (TDependencySpace *) FreeIfObject(gMacAppDependencies);
-
- fPendingReplyList = (TEventList *) FreeListIfObject(fPendingReplyList);
-
- // This rigamarole is necessary because TEvent::Free() checks to see whether
- // the event is still on the event list.
- if (fEventList != NULL)
- {
- TEvent * anEvent;
-
- while ((anEvent = (TEvent *) fEventList->Last()) != NULL)
- {
- fEventList->AtDelete(fEventList->GetSize());
- anEvent->Free();
- }
-
- fEventList->Free();
- }
-
- fDocumentList = (TList *) FreeListIfObject(fDocumentList);
-
- // Must do this -before- freeing fFreeWindowList
- TView * theDeskScrapView = gClipboardMgr->fClipOrphanage;
- if (theDeskScrapView != NULL)
- {
- // Because TDeskScrapView overrides Free to -not- call inherited::Free
- theDeskScrapView->TView::Free();
-
- if (gClipboardMgr->fClipView == theDeskScrapView)
- gClipboardMgr->fClipView = NULL;
- gClipboardMgr->fClipOrphanage = NULL;
- }
-
- fFreeWindowList = (TList *) FreeListIfObject(fFreeWindowList);
- }
-
-
- #pragma segment AClose
- void TTidyApplication::CleanUpJustInTimeObjects()
- {
- // gWindows is created on a "just in time" basis by RegisterWindow() in UWindows.cp.
- // It seems to be the only object that MacApp creates in this fashion.
-
- extern TDynamicArray * gWindows;
- gWindows = (TDynamicArray *) FreeIfObject(gWindows);
- }
-
-
- #pragma segment AClose
- void TTidyApplication::Free() // Override
- {
- /******
-
- Some of the objects which MacApp 3.0.1 allocates are freed by TApplication::Close(),
- which is called from TQuitCommand::DoIt(). Some others are freed when CleanUpMacApp()
- calls gApplication->Free(), resulting in calls to TCommandHandler::Free() and
- TEventHandler::Free(). Others are not freed at all; they disappear when the
- application's heap is disposed of after the application terminates.
-
- This set of functions endeavors to clean up what MacApp doesn't. One reason to
- do so is that one can set a breakpoint just before the application terminates
- and learn from the results. Any objects left are objects one's own application
- has allocated but not freed. Some of these may span the life of the application
- and need not be freed except to make the rest stand out (as above); others
- represent likely memory leaks.
-
- *******/
-
- #ifdef qCleanUpAtQuit
- this->CleanUpAfterIApplication();
- this->CleanUpJustInTimeObjects();
-
- DeInitUMacApp();
- #endif
-
- this->TApplication::Free();
-
- // Be sure not to refer to members of gApplication
- // after calling TApplication::Free()!
-
- #ifdef qCleanUpAtQuit
- LastChance();
- #endif
- }